package com.kangaroo.myhome.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kangaroo.myhome.model.ModdifyPasword;
import com.kangaroo.myhome.model.TSysUser;
import com.kangaroo.myhome.service.impl.TSSysUserService;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

/**
 * @Description: 功能描述: //
 * @author: kangaroo
 * @date: 2020/7/31 0:02
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private TSSysUserService tSSysUserService;

    // @PreAuthorize("hasAuthority('sys:user:view')")
    @GetMapping(value = "/findAll")
    public ResponseModel findAll(HttpServletRequest request) {
        String account = request.getParameter("account");
        String username = request.getParameter("username");
        String pageS = request.getParameter("page");
        String rowsS = request.getParameter("rows");
        int page = Integer.parseInt(pageS);
        int rows = Integer.parseInt(rowsS);
        System.out.println("1111111111111");
        return tSSysUserService.findAll(account, username, page, rows);
    }

    // @PreAuthorize("hasAuthority('sys:user:edit')")
    @PostMapping(value = "/edit")
    public ResponseModel edit(@RequestBody TSysUser tSysUser) {

        return tSSysUserService.edit(tSysUser);
    }

    // @PreAuthorize("hasAuthority('sys:user:edit')")
    @GetMapping(value = "/view")
    public ResponseModel view(HttpServletRequest request) {
        ResponseModel model = new ResponseModel();
        String id = request.getParameter("id");

        TSysUser tSysUser = new TSysUser();
        tSysUser.setId(id);
        TSysUser user = tSSysUserService.view(tSysUser);
        user.setPassword("");
        model.setCode(200);
        model.setMessage("获取成功");
        model.setData(user);
        return model;
    }

    // @PreAuthorize("hasAuthority('sys:user:delete')")
    @PostMapping(value = "/delete")
    public ResponseModel delete(HttpServletRequest request) {
        String id = request.getParameter("id");
        TSysUser tSysUser = new TSysUser();
        tSysUser.setId(id);
        tSSysUserService.delete(tSysUser);
        ResponseModel model = new ResponseModel();
        model.setCode(200);
        model.setMessage("获取成功");
        return model;
    }

    @PostMapping(value = "/modifyPassword")
    public ResponseModel modifyPassword(@RequestBody ModdifyPasword moddifyPasword) {
        ResponseModel model = new ResponseModel();
        if (StringUtil.isEmpty(moddifyPasword.getAccount())
                || StringUtil.isEmpty(moddifyPasword.getPassword())
                || StringUtil.isEmpty(moddifyPasword.getRePassword())) {
            model.setCode(201);
            model.setMessage("用户名或密码错误");
            return model;
        } else {
            return tSSysUserService.modifyPassword(moddifyPasword);
        }
    }

    @PostMapping(value = "/selectUserByCode")
    public ResponseModel selectUserByCode(@RequestParam String username) {
        ResponseModel model = new ResponseModel();
        TSysUser user = tSSysUserService.selectUserByCode(username);
        System.out.println("1111111111111"+ user.getUserName());
        user.setPassword("");
        model.setData(user);
        return model;
    }

}